public class Ninja {

  int fingerId = -1;
  float durationBetweenEachFrame;
  int previousUpdate;
  int currentImage = 0;
  PImage stillImages[], movingImages[];
  float scaleValue = 1;
  float xScale = 1.0, yScale = 1.0;
  PVector startingLocation;
  PVector velocity = new PVector(0,0);
  boolean isMoving = false, isSwinging = false;;
  PVector destination = new PVector(0,0);
  PVector currentLocation = new PVector(0,0);
  float xDistance, yDistance, radius, attackRadius;
  PImage ring1 = loadImage("movementRing.png");
  PImage ring2 = loadImage("AttackCircle.png");
  float currentAngle = 0;
  PImage action1[];
  int actionImage = 0;
  int lastSwing = 0;
  float previousMoveTime = 0, durationBetweenEachMove;
  float radiusOfCastle = 162*width/1920.0;
  PVector center = new PVector(width/2.0, height/2.0);
  
  public Ninja(PImage stillImages[], PImage movingImages[], PImage action1[], float durationBetweenEachFrame, float durationBetweenEachMove, PVector startingLocation, float radius, float attackRadius){
    previousUpdate = millis();
    this.durationBetweenEachFrame = durationBetweenEachFrame;
    this.stillImages = stillImages;
    this.movingImages = movingImages;
    this.startingLocation = startingLocation;
    this.currentLocation.add(startingLocation);
    this.radius = radius;
    this.attackRadius = attackRadius;
    this.action1 = action1;
    this.durationBetweenEachMove = durationBetweenEachMove;
  }

  void setScale(float xScale, float yScale){
    this.xScale = xScale;
    this.yScale = yScale;
  }

  boolean isAttackPointInRange(PVector touch){
    if(touch.dist(currentLocation) < attackRadius){
      return true;
    } else {
      return false; 
    }
  }

  void update(int currentTime){
    if(currentTime - previousUpdate > durationBetweenEachFrame){
      if(currentImage >= stillImages.length-1){
        currentImage = 0;  
      }
      else {
        currentImage++;
      }
      previousUpdate = currentTime;
    }
    pushMatrix();
    translate(currentLocation.x, currentLocation.y);
    scale(.6, .6);
    rotate(currentAngle);
    image(stillImages[currentImage], 0, 0);
    popMatrix();
    showRings();
  }
  
  void startSwing(int currentTime){
    isSwinging = true;
    isMoving = false;
    lastSwing = currentTime;
  }
  
  void swingSword(int currentTime){
    if(currentTime - lastSwing > 50){
      if(actionImage >= action1.length-1){
        actionImage = 0;  
        isSwinging = false;
      }
      else {        
        actionImage++;
      }
      lastSwing = currentTime;
    }
    pushMatrix();
    translate(currentLocation.x, currentLocation.y);
    scale(.6, .6);
    rotate(currentAngle);
    image(action1[actionImage], 0, 0);
    popMatrix();
    showRings();
  }

  boolean isMoving(){
    return isMoving; 
  }
  
  boolean isSwinging(){
   return isSwinging; 
  }

  void setFingerId(int id){
    fingerId = id; 
  }

  int getFingerId(){
    return fingerId; 
  }
  
  boolean isPointInRange(PVector touch){
    if(touch.dist(currentLocation) < radius){
      return true;
    } else {
      return false; 
    }
  }
  
  void setCurrentAngle(float angle){
   currentAngle = angle; 
  }

  void setVelocity(PVector destination){
    float maxVelocity = 4;
    startingLocation.x = currentLocation.x;
    startingLocation.y = currentLocation.y;
    this.destination = destination; 
    xDistance = destination.x - startingLocation.x;
    yDistance = destination.y - startingLocation.y;
    if((xDistance == 0) && (yDistance == 0)) {
      isMoving = false;
    } else {
      isMoving = true;
      isSwinging = false; 
      float currentRadius = destination.dist(currentLocation);
      if((xDistance < 0) && (yDistance > 0)){
        currentAngle = PI/2 + acos(xDistance/currentRadius);
      } 
      else if ((xDistance < 0) && (yDistance < 0)){
        currentAngle = 5*PI/2 - acos(xDistance/currentRadius);
      } 
      else if ((xDistance > 0) && (yDistance < 0)) {
        currentAngle = PI/2 - acos(xDistance/currentRadius);
      } 
      else if((xDistance > 0) && (yDistance > 0)) {
        currentAngle = PI/2 + acos(xDistance/currentRadius);
      }
      
      if(xDistance < 0){
        velocity.x = -maxVelocity*(abs(xDistance)/(abs(yDistance) + abs(xDistance)));
      } else {
        velocity.x = maxVelocity*(abs(xDistance)/(abs(yDistance) + abs(xDistance))); 
      } 
      
      if(yDistance < 0){
        velocity.y = -maxVelocity*(abs(yDistance)/(abs(yDistance) + abs(xDistance)));
      } else {
        velocity.y = maxVelocity*(abs(yDistance)/(abs(yDistance) + abs(xDistance))); 
      }   
    }
  }
  
  private void showRings(){    
    pushMatrix();
    translate(currentLocation.x, currentLocation.y);
    scale(1.5, 1.5);
    image(ring1, 0,0);
    popMatrix();
    image(ring2, currentLocation.x, currentLocation.y);
  }
  void checkIfOutOfBounds(){
    if(currentLocation.x > (width-50)) {
      velocity.x = 0;
      velocity.y = 0; 
      isMoving = false;
      startingLocation.x = currentLocation.x-11;
      startingLocation.y = currentLocation.y;
      currentLocation.x -= 11;
    } 
    else if (currentLocation.x < 0){
      velocity.x = 0;
      velocity.y = 0; 
      isMoving = false;
      startingLocation.x = currentLocation.x+11;
      startingLocation.y = currentLocation.y;
      currentLocation.x += 11;
    } 
    else if (currentLocation.y < (0)){
      velocity.x = 0;
      velocity.y = 0; 
      isMoving = false;
      startingLocation.x = currentLocation.x;
      startingLocation.y = currentLocation.y+11;      
      currentLocation.y += 11;      
    } 
    else if (currentLocation.y > (height - 50)) {
      velocity.x = 0;
      velocity.y = 0; 
      isMoving = false;
      startingLocation.x = currentLocation.x;
      startingLocation.y = currentLocation.y-11;
      currentLocation.y -= 11;
    }
  }
  
  private void checkIfNearCaslte(){
    if(currentLocation.dist(center) < radiusOfCastle){
      if((currentLocation.x > center.x) && (currentLocation.y > center.y)){
       currentLocation.x += 2; 
       currentLocation.y += 2;
      } else if((currentLocation.x > center.x) && (currentLocation.y < center.y)){
       currentLocation.x += 2; 
       currentLocation.y -= 2;
      } else if((currentLocation.x < center.x) && (currentLocation.y > center.y)){
       currentLocation.x -= 2; 
       currentLocation.y += 2;
      } else if((currentLocation.x < center.x) && (currentLocation.y < center.y)){
       currentLocation.x -= 2; 
       currentLocation.y -= 2;
      }
    }
  }

  void moveToAreaThroughAnimation(int currentTime){
    float xDestination = destination.x;
    float yDestination = destination.y;
    xDistance = destination.x - startingLocation.x;
    yDistance = destination.y - startingLocation.y;
    if(currentTime - previousMoveTime > durationBetweenEachMove){
      currentLocation.add(velocity);
      checkIfOutOfBounds();
      checkIfNearCaslte();
      previousMoveTime = currentTime;
    } 
    if(currentTime - previousUpdate > durationBetweenEachFrame){
      if(currentImage >= movingImages.length-1){
        currentImage = 0;  
      }
      else {
        currentImage++;
      }
      previousUpdate = currentTime;
    }
    pushMatrix();
    translate(currentLocation.x, currentLocation.y);
    scale(.6, .6);
    rotate(currentAngle);
    image(movingImages[currentImage], 0, 0);
    popMatrix();
    showRings();
//    if(velocity.x > 0)  { 
//      if((xDestination - currentLocation.x) < 0) {
//        if(velocity.y > 0){
//          if((yDestination - currentLocation.y)  < 0) {
//            velocity.x = 0;
//            velocity.y = 0; 
//            isMoving = false;
//            startingLocation.x = xDestination;
//            startingLocation.y = yDestination;
//          }
//        } 
//        else {
//          if((yDestination - currentLocation.y)  > 0) {
//            velocity.x = 0;
//            velocity.y = 0; 
//            isMoving = false;
//            startingLocation.x = xDestination;
//            startingLocation.y = yDestination;
//          }
//        }
//      }
//    } 
//    else {
//      if((xDestination - currentLocation.x) > 0){
//        if(velocity.y > 0){
//          if((yDestination - currentLocation.y)  < 0) {
//            velocity.x = 0;
//            velocity.y = 0; 
//            isMoving = false;
//            startingLocation.x = xDestination;
//            startingLocation.y = yDestination;
//          }
//        } 
//        else {
//          if((yDestination - currentLocation.y)  > 0) {
//            velocity.x = 0;
//            velocity.y = 0; 
//            isMoving = false;
//            startingLocation.x = xDestination;
//            startingLocation.y = yDestination;
//          }
//        }
//      } 
    
  }
}



